1   /*
2    * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
3    * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4    *
5    * This code is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU General Public License version 2 only, as
7    * published by the Free Software Foundation.
8    *
9    * This code is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11   * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12   * version 2 for more details (a copy is included in the LICENSE file that
13   * accompanied this code).
14   *
15   * You should have received a copy of the GNU General Public License version
16   * 2 along with this work; if not, write to the Free Software Foundation,
17   * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18   *
19   * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20   * or visit www.oracle.com if you need additional information or have any
21   * questions.
22   */
23  
24  /*
25   * @test
26   * @bug 6987827
27   * @summary security/util/Resources.java needs improvement
28   */
29  
30  
31  import java.lang.reflect.Method;
32  import java.util.HashSet;
33  import java.util.Set;
34  
35  /**
36   * This test makes sure that the keys in resources files are using the new
37   * format and there is no duplication.
38   */
39  public class NewNamesFormat {
40      public static void main(String[] args) throws Exception {
41          checkRes("sun.security.util.Resources");
42          checkRes("sun.security.util.AuthResources");
43          checkRes("sun.security.tools.JarSignerResources");
44      }
45  
46      private static void checkRes(String resName) throws Exception {
47          System.out.println("Checking " + resName + "...");
48          Class clazz = Class.forName(resName);
49          Method m = clazz.getMethod("getContents");
50          Object[][] contents = (Object[][])m.invoke(clazz.newInstance());
51          Set<String> keys = new HashSet<String>();
52          for (Object[] pair: contents) {
53              String key = (String)pair[0];
54              if (keys.contains(key)) {
55                  System.out.println("Found dup: " + key);
56                  throw new Exception();
57              }
58              checkKey(key);
59              keys.add(key);
60          }
61      }
62  
63      private static void checkKey(String key) throws Exception {
64          for (char c: key.toCharArray()) {
65              if (Character.isLetter(c) || Character.isDigit(c) ||
66                      c == '{' || c == '}' || c == '.') {
67                  // OK
68              } else {
69                  System.out.println("Illegal char [" + c + "] in key: " + key);
70                  throw new Exception();
71              }
72          }
73      }
74  }